package nku.csc364.updatedatabase;

import nku.csc364.R;
import nku.csc364.databasehelper.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class UpdateDatabaseCategoryActivity extends Activity{
	
	DataHelper dh = null; 
	Spinner categorySpinner;
	EditText categoryName, categoryDesc;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.category);
        
        categorySpinner = (Spinner)findViewById(R.id.categorySpinner1);
        categoryName = (EditText)findViewById(R.id.categoryEditText1);
        categoryDesc = (EditText)findViewById(R.id.categoryEditText2);
        
        dh = DataHelper.getDataHelper(this);
        setCategorySpinner();
        categorySpinner.setPrompt("Select...");
        categorySpinner.setOnItemSelectedListener(new MyOnCategorySelectedListener());
    }
    
	public  void openCategory(View view){
		Intent launchCategory = new Intent(this, UpdateDatabaseCategoryActivity.class);
		startActivityForResult(launchCategory, 1);
	}
	
	public void openItem(View view){
		Intent launchItem = new Intent(this, UpdateDatabaseItemActivity.class);
		startActivityForResult(launchItem, 1);
	}
	
	public void openProduct(View view){
		Intent launchProduct = new Intent(this, UpdateDatabaseProductActivity.class);
		startActivityForResult(launchProduct, 1);
	}
	
	public void openQOH(View view){
		Intent launchQOH = new Intent(this, UpdateDatabaseQOHActivity.class);
		startActivityForResult(launchQOH, 1);
	}
	
	public void openSupplier(View view){
		Intent launchSupplier = new Intent(this, UpdateDatabaseSupplierActivity.class);
		startActivityForResult(launchSupplier, 1);
	}
    
    public void doAddCategory(View view){
    	dh.insertCategory(categoryName.getText().toString(), categoryDesc.getText().toString());
    }
    
    public void doDeleteCategory(View view){
    	dh.deleteCategory(categorySpinner.getSelectedItem().toString());
    }
    
    public void doUpdateCategory(View view){
    	this.doDeleteCategory(view);
    	this.doAddCategory(view);
    	
    }
    
	  @SuppressWarnings("unchecked")
	public ArrayAdapter<CharSequence> getCategoryAdapter(){
			    @SuppressWarnings("rawtypes")
				ArrayAdapter itemAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, dh.selectAllCategorys());	 
		        itemAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		        itemAdapter.insert(" ", 0);
		        return itemAdapter;
	}
	  
	public void setCategorySpinner(){
		  categorySpinner.setAdapter(getCategoryAdapter());
	}
	
	private void clean() {
		this.categorySpinner.setSelection(0);
		categoryName.setText("");
		categoryDesc.setText("");
	}
	
	  public class MyOnCategorySelectedListener implements OnItemSelectedListener {
		    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		    	if(!parent.getItemAtPosition(pos).toString().equals(" ")){
		    		Cursor mCursor = dh.getCategory(parent.getItemAtPosition(pos).toString());
			    	int i = 0;
			    	String name =  "";
			    	String description = "";
					for(String x : mCursor.getColumnNames()){
						if(i == 0){
							name = mCursor.getString(mCursor.getColumnIndexOrThrow(x));	
						}
						else if( i == 1){
							description = mCursor.getString(mCursor.getColumnIndexOrThrow(x));	
						}
						i++;
					}
					categoryName.setText(name);
					categoryDesc.setText(description);
			    }
		    	else{
		    		clean();
		    	}
		    }
		    public void onNothingSelected(@SuppressWarnings("rawtypes") AdapterView parent) {
		      // Do nothing.
		    }
	}
}
